<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 16/8/4
 * Time: 09:13
 */

namespace Admin\Controller;


use Uclient\Api\UserApi;
use Admin\Api\MemberApi;
use Admin\Api\AuthGroupAccessApi;
use Recruit\Api\UcenterMemberApi;

class PersonController extends AdminController {

    /**
     * 个人中心
     * 不同角色调用不同的页面
     */
    public function center(){
        $user =  session("global_user");
        $id = $user['id'];


        $result = apiCall(AuthGroupAccessApi::QUERY_GROUP_INFO, array($id));
        if(!$result['status']){
            $this->error($result['info']);
        }
        $userRoles = $result['info'];

        $this->assign("userroles",$userRoles);
        $role_ids = "";
        for($i=0;$i < count($userRoles);$i++){
            $role_ids .= '_'.$userRoles[$i]['group_id'];
        }
        $tplFileName = "center".$role_ids;
        $path = realpath(APP_PATH."/".MODULE_NAME."/View/default/".CONTROLLER_NAME."/").'/';

        if(!file_exists($path.$tplFileName.".html")) {
            $tplFileName = "";
        }


        $this->display($tplFileName);
        


    }

    /*
     * 后台操作用信息管理和修改
     * */
    public function info(){
        $map['itboye_ucenter_member.id'] =UID;
        $fields = 'itboye_ucenter_member.id,itboye_ucenter_member.username,itboye_ucenter_member.mobile,itboye_ucenter_member.email,itboye_ucenter_member.reg_time,itboye_ucenter_member.last_login_time,common_member.realname,common_member.sex';
        $info = apiCall(UcenterMemberApi::JOIN_AUTH_GET,array($map,false,$fields));
        if($info['status']){
            $this->assign($info['info']);
        }
        $this->display();
    }
    /*
     *
     * */
    public function edit(){
        if(IS_POST){
            //开启事务
            $modal = M();
            $modal -> startTrans();
            $flag = false;
            $entity['email'] = I('email');
            $result = apiCall(UserApi::SAVE_BY_ID, array(I('id'),$entity));
            if($result['status']){
                $membermap['uid'] = I('id');
                $membersave = array(
                    'nickname'=>I('nickname'),
                    'realname'=>I('nickname'),
                    'sex'=>I('sex'),
                );
                $result = apiCall(MemberApi::SAVE_BY_ID, array($membermap['uid'],$membersave));
                if(!$result['status']){
                    $flag = true;
                }
            }else{
                $flag = true;
            }
            if($flag){
                $modal -> rollback();
                $this->error('修改失败！');
            }else{
                $modal->commit();
                $this->success('修改成功！',U('Person/info'));
            }
        }else{
            $map['itboye_ucenter_member.id'] = I('id');
            $fields ='itboye_ucenter_member.id,itboye_ucenter_member.username,itboye_ucenter_member.email,common_member.nickname,common_member.sex';
            $info = apiCall(UcenterMemberApi::JOIN_AUTH_GET,array($map,'last_login_time desc',$fields));
            if($info['status']){
                $this->assign($info['info']);
            }
            $this->display();
        }
    }
}